<?php
namespace App\Http\Controllers\Backstage;
use App\Http\Controllers\Controller;
use App\Http\Controllers\Validator\MoveRuleVali;
use App\Http\Controllers\Validator\RoomInfoVail;
use App\Http\Controllers\Validator\RoomWordbookVail;
use App\Model\Account\Type;
use App\Model\Backstage\Customers;
use App\Model\Backstage\MoveRule;
use App\Model\Backstage\MoveRuleStatus;
use App\Model\Backstage\RoomInfo;
use App\Model\Backstage\RoomWordbook;
use App\Model\Backstage\RoomWordbookInfo;
use App\Model\Backstage\User;
use App\Tool\Error;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
use Validator;
use Illuminate\Support\Facades\Auth;

/**
 * Class MoveRuleController
 * @package App\Http\Controllers\Backstage
 * 公司房客自动转移规则
 * date:2018-4-29
 */
class MoveRuleController extends Controller
{

    /**
     * @return \Illuminate\Http\JsonResponse
     * 添加页面所需动态数据
     */
    public function dataList(){
        $data = [
            'move_type' => MoveRule::$moveTypeArr,
            'date_type' => MoveRule::$dateTypeArr
        ];
        return $this->success('ok', $data);
    }

    /**
     * @param MoveRule $moveRule
     * @return \Illuminate\Http\JsonResponse
     * 规则数据列表
     */
    public function lists(MoveRule $moveRule){
        $list = $moveRule->lists();
        if(!$list){
            return $this->error(Error::NULL_DATA);
        }
        //dd($list);
        foreach($list as $k => $v){
            $v->a_date_type_name = MoveRule::$dateTypeArr[$v->a_date_type]; //日期类型
            $v->a_move_type_name = MoveRule::$moveTypeArr[$v->a_move_type]; //转移类型
            $v->a_trade_type_name = Type::getTypeDetail($v->a_trade_type); //交易类型
            $v->a_m_status_name   = Type::getTypeDetail($v->a_m_status); //房源状态
            $v->a_use_name        = Type::getTypeDetail($v->a_use_type); //用途
            $entrust_way_id = explode(',', $v->a_entrust_way);  //委托方式多个以逗号隔开
            $entrust_way_arr = [];
            foreach($entrust_way_id as $value){
                $temp_arr['id'] = $value;
                $temp_arr['name'] = Type::getTypeDetail($value);
                array_push($entrust_way_arr, $temp_arr);
            }
            $v->a_entrust_way     = $entrust_way_arr;
            $v->a_status_name     = $v->a_status = 1 ? '启用' : '禁用';
        }
        return $this->success('ok', $list);

    }

    /**
     * @param Request $request
     * @param MoveRule $moveRule
     * @return \Illuminate\Http\JsonResponse
     * 添加规则
     */
    public function addRule(Request $request, MoveRule $moveRule){
        $params = $request->except('token');
        if(!MoveRuleVali::validate($params)){
            return $this->error(Error::PARAM_MISSING);
        }
         if($moveRule->addRule($params)){
             return $this->success('ok', '添加成功！');
         }
        return $this->error(Error::ADD_ERROR);

    }
    /**
     * @param Request $request
     * @param MoveRule $moveRule
     * @return \Illuminate\Http\JsonResponse
     * 编辑规则
     */
    public function editRule(Request $request, MoveRule $moveRule){
        $params = $request->except('token');
        if(!MoveRuleVali::validate($params) || !$request->input('a_id')){
            return $this->error(Error::PARAM_MISSING);
        }
        if($moveRule->updateRule($params)){
            return $this->success('ok', '修改成功！');
        }
        return $this->error(Error::EDIT_ERROR);
    }

    /**
     * @param Request $request
     * @param MoveRule $moveRule
     * @return \Illuminate\Http\JsonResponse
     * 删除规则
     */
    public function delRule(Request $request, MoveRule $moveRule){
        if(!$request->input('a_id')){
            return $this->error(Error::PARAM_MISSING);
        }
        if($moveRule->delRule($request->input('a_id'))){
            return $this->success('ok', '删除成功！');
        }
        return $this->error(Error::DEL_ERROR);
    }

    /**
     *用于手动即时更新规则调用
     */
    public function immediateSave(Request $request, RoomInfo $roomInfo, Customers $customers){
        if(!$request->input('a_id')){
            return $this->error(Error::PARAM_MISSING);
        }
        $info = MoveRule::getInfo($request->input('a_id'));
        //dd($info);
        if(!$info){
            return $this->error(Error::NULL_DATA);
        }
        $res = false;
        if($info['a_move_type'] == 0 || $info['a_move_type'] == 2){  //私盘转公盘  取消优质房
            $res = $roomInfo->updateRuleDisk($info);
        }elseif($info['a_move_type'] == 1){  //私客转公客
            $res = $customers->updateRuleCustomers($info);
        }
        if($res){
            return $this->success('ok', '操作成功！');
        }
        return $this->error(Error::OPERATION_ERROR);
    }
    /**
     * @param MoveRuleStatus $moveRuleStatus
     * @param MoveRule $moveRule
     * @param RoomInfo $roomInfo
     * @param Customers $customers
     * @return \Illuminate\Http\JsonResponse
     * 此接口需要员工登陆完成后调用此方法
     * 执行公司自定义房客更新规则
     */
    public function updateRule(MoveRuleStatus $moveRuleStatus, MoveRule $moveRule, RoomInfo $roomInfo, Customers $customers)
    {
        $res = $moveRuleStatus->getStatus($moveRule, $roomInfo, $customers);
        if($res){
            return $this->success('ok');
        }
        return $this->error(Error::EDIT_ERROR);
    }

    /**
     * @return \Illuminate\Http\JsonResponse
     * 转出类目列表数据
     */
    public function moveData(){
        return $this->success('ok',MoveRule::$moveListArr);
    }

    /**
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     * 员工转房客
     */
    public function moves(Request $request, MoveRule $moveRule){
        $params = $request->except('token');
        if(!MoveRuleVali::moveValidate($params)){
            return $this->error(Error::PARAM_MISSING);
        }
        $res = $moveRule->moves($params);
        if($res){
            return $this->success('ok', '操作成功');
        }
        return $this->error(Error::OPERATION_ERROR);
    }

    /**
     * @return \Illuminate\Http\JsonResponse
     * 转换页面需要下拉数据
     */
    public function turnData(){
        $data = [
            'turntype' => MoveRule::$turnArr,
            'datetype' => MoveRule::$dateTypeArr
        ];
        return $this->success('ok', $data);
    }

    /**
     * @param Request $request
     * 员工公私盘 公私客相互转换
     */
    public function turns(Request $request, MoveRule $moveRule){
        $params = $request->except('token');
        if(!MoveRuleVali::turnValidate($params)){
            return $this->error(Error::PARAM_MISSING);
        }
        if($moveRule->turns($params)){
            return $this->success('ok', '操作成功！');
        }
        return $this->error(Error::OPERATION_ERROR);
    }


}